package com.neoCalf.neoUser.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neoCalf.neoCommon.entity.UserInfo;
import com.neoCalf.neoCommon.enums.JoinTypeEnum;
import com.neoCalf.neoCommon.enums.ResultCode;
import com.neoCalf.neoCommon.enums.UserContactTypeEnum;
import com.neoCalf.neoCommon.enums.ZeEnum;
import com.neoCalf.neoCommon.exception.BusinessException;
import com.neoCalf.neoCommon.model.dto.UserInfoDto;
import com.neoCalf.neoCommon.utils.DataTool;
import com.neoCalf.neoCommon.utils.Md5Util;
import com.neoCalf.neoUser.feigns.ContactServiceFeign;
import com.neoCalf.neoUser.feigns.GroupServiceFeign;
import com.neoCalf.neoUser.mapper.UserInfoMapper;
import com.neoCalf.neoUser.pro.SysSettingPro;
import com.neoCalf.neoUser.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import javax.annotation.Resource;


/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/8 11:33
 */
@Service(value = "userInfoService")
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Resource
    private SysSettingPro sysSettingPro;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Autowired
    private ContactServiceFeign contactServiceFeign;
    @Autowired
    private GroupServiceFeign groupServiceFeign;

    @Override
    public void initRobotAccount() {
        boolean result = saveOrUpdate(new UserInfo(
                sysSettingPro.getRobotUid(),
                sysSettingPro.getRobotUid(),
                sysSettingPro.getRobotNick(),
                JoinTypeEnum.JOIN.getJoinType(),
                sysSettingPro.getRobotAvatar()
        ));
        if (!result) {
            throw new RuntimeException("--机器人账户初始化失败--");
        }
        log.info("--机器人账户初始化成功--");
    }

    @Override
    public UserInfoDto findDetailUserInfo(String userId) {
        UserInfo userInfo = getById(userId);
        if (userInfo == null) {
            return null;
        }
        Long meCount = groupServiceFeign.myGroupCount(userId).getResult();
        Long joinCount = contactServiceFeign.findMyContactCount(
                userId,
                new Integer[]{
                        UserContactTypeEnum.GROUP.getType()
                }).getResult();
        Long friendCount = contactServiceFeign.findMyContactCount(
                userId,
                new Integer[]{
                        UserContactTypeEnum.USER.getType(),
                        UserContactTypeEnum.ROBOT.getType(),
                        UserContactTypeEnum.SYSTEM.getType()
                }).getResult();
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUserInfo(userInfo);
        userInfoDto.setMeGroupMemberCount(meCount);
        userInfoDto.setJoinGroupMemberCount(joinCount);
        userInfoDto.setFriendCount(friendCount);
        return userInfoDto;
    }

    @Override
    public void updateUserInfo(String userId, UserInfo userInfo) {
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserInfo::getUserId, userId);
        updateWrapper.set(!userInfo.getNickName().isEmpty(), UserInfo::getNickName, userInfo.getNickName());
        updateWrapper.set(userInfo.getSex() != null, UserInfo::getSex, userInfo.getSex());
        updateWrapper.set(userInfo.getPersonalSignature() != null, UserInfo::getJoinType, userInfo.getJoinType());
        updateWrapper.set(!userInfo.getPersonalSignature().isEmpty(), UserInfo::getAreaCode, userInfo.getAreaCode());
        updateWrapper.set(!userInfo.getPersonalSignature().isEmpty(), UserInfo::getAreaName, userInfo.getAreaName());
        updateWrapper.set(!userInfo.getPersonalSignature().isEmpty(), UserInfo::getPersonalSignature, userInfo.getPersonalSignature());
        int update = userInfoMapper.update(null, updateWrapper);
        DataTool.checkAffectRow(update, "更新用户信息失败");
    }

    @PutMapping("updatePassword")
    public void updatePassword(String userId,
                               String oldPassword,
                               String password) {
        if (!DataTool.zeCheckStr(ZeEnum.ZE_PASSWORD, new String[]{oldPassword, password})) {
            throw new BusinessException(ResultCode.CODE_10017, "密码格式错误");
        }
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserInfo::getUserId, userId);
        updateWrapper.eq(UserInfo::getPassword, Md5Util.encodeByMd5(oldPassword));
        updateWrapper.set(UserInfo::getPassword, Md5Util.encodeByMd5(password));
        int update = userInfoMapper.update(null, updateWrapper);
        DataTool.checkAffectRow(update, "更新密码失败");
    }

    @Override
    public void saveAvatar(String userId, String url) {
        LambdaUpdateWrapper<UserInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserInfo::getUserId, userId);
        updateWrapper.set(UserInfo::getAvatar, url);
        int update = userInfoMapper.update(null, updateWrapper);
        DataTool.checkAffectRow(update, "更新头像失败");
    }
}
